var table;
var $;
var layer;
var form;

var all=0;
var exmamineTrue=0;
var waitExamine=0;
var exmamineFalse=0;
var count=0;
layui.use(['table','jquery','layer','form'],function () {
    table = layui.table;
    $ = layui.jquery;
    layer = layui.layer;
    form = layui.form;

    $("#b1").removeClass("layui-btn-primary");
    $("#b1").addClass("layui-bg-cyan");
    $("#b1").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $("#b1").removeClass("layui-btn-primary");
        $("#b1").addClass("layui-bg-cyan");
    })
    $("#b2").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $(".buttons").removeClass("layui-bg-cyan");
        $("#b2").addClass("layui-bg-cyan");
    })
    $("#b3").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $(".buttons").removeClass("layui-bg-cyan");
        $("#b3").addClass("layui-bg-cyan");
    })
    $("#b4").click(function () {
        $(".buttons").addClass("layui-btn-primary");
        $(".buttons").removeClass("layui-bg-cyan");
        $("#b4").addClass("layui-bg-cyan");
    })


    var tablereload = function tablereload(data) {
        tableIns = table.render({
            elem: '#table',
            data: data,
            title: '数据列表',
            limit: 6,
            id: 'table',
            toolbar: '#toptool',
            page: {
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: "首页",
                last: "尾页",
            },
            size: 'lg',
            cols:[[
                {type:'numbers',title:'序号',align:'center'},
                {field:'productCode',title:'商品编号',align:'center'},
                {field:'mainImage',title:'商品图片',align:'center',templet:function (data) {
                        return '<img src="'+"http://"+data.mainImage+'"/>';
                    }},
                {field:'productName',title:'商品名称',align:'center'},
                {field:'currentpriceProduct',title:'价格',align:'center'},
                {field:'title',title:'分类',align:'center'},
                {field:'createName',title:'上传人员',align:'center'},
                {field:'result',title:'审核状态',align:'center',templet:function (data) {
                        if(data.result=="1"){
                            return "审核通过";
                        }else if(data.result=="2"){
                            return "未审核";
                        }else{
                            return "审核未通过";
                        }
                    }},
                {title:'操作',align:'center',templet:function (data) {
                        if(data.result=="1"|data.result=="3"){
                            return "<div>" +
                                "<a href='javascript:chakan("+data.productId+");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                                "</div>";
                        }else{
                            return "<div>" +
                                "<a href='javascript:chakan("+data.productId+");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                                "<a href='javascript:shenhe(\""+data.productId+"\",\""+data.productName+"\");' style='color: #0EAA9E'>审核</a><br/>"+
                                "</div>";
                        }
                    }}
            ]],
        });
    }

    /*下拉框*/
    getChanName();
    function getChanName() {
        $.ajax({
            url: "/goodsTable/getGoodsClass",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var d = eval(data.data);
                console.log(data.data);
                $("#title").append("<option value=''></option>");
                for(var i = 0; i < d.length-1; i++) {
                    $("#title").append("<option value='" + d[i].categroyId + "'>" + d[i].title + "</option>");
                }
                form.render('select');
            }
        })
    }
    /*获取审核表信息*/
    getExamineTable("/goodsTable/getExamineTable?type="+3);
    function getExamineTable(url){
        table.render({
            elem:"#table",
            url:url,
            toolbar:'#toptool',
            defaultToolbar:['print','filter','exports'],
            size:"lg",
            page:true,
            limit:5,
            cols:[[
                {type:'numbers',title:'序号',align:'center'},
                {field:'productCode',title:'商品编号',align:'center'},
                {field:'mainImage',title:'商品图片',align:'center',templet:function (data) {
                        return '<img src="'+"http://"+data.mainImage+'"/>';
                    }},
                {field:'productName',title:'商品名称',align:'center'},
                {field:'currentpriceProduct',title:'价格',align:'center'},
                {field:'title',title:'分类',align:'center'},
                {field:'createName',title:'上传人员',align:'center'},
                {field:'result',title:'审核状态',align:'center',templet:function (data) {
                        if(data.result=="1"){
                            all++;
                            exmamineTrue++;
                            return "审核通过";
                        }else if(data.result=="2"){
                            all++;
                            waitExamine++;
                            return "待审核";
                        }else{
                            all++;
                            exmamineFalse++;
                            return "审核未通过";
                        }
                    }},
                {title:'操作',align:'center',templet:function (data) {
                        if(data.result=="1"|data.result=="3"){
                            return "<div>" +
                                "<a href='javascript:chakan("+data.productId+");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                                "</div>";
                        }else{
                            return "<div>" +
                                "<a href='javascript:chakan("+data.productId+");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                                "<a href='javascript:shenhe(\""+data.productId+"\",\""+data.productName+"\");' style='color: #0EAA9E'>审核</a><br/>"+
                                "</div>";
                        }
                    }}
            ]]
        });
    }




    /*不同的状态*/
    $("#b1").click(function () {
        location.reload();
        changeColor1();
    });

    $("#b2").click(function () {
        var result='1';
        getExamineTable("/goodsTable/getExamineTable?result="+result+"&type="+3);
        changeColor2();
    });

    $("#b3").click(function () {
        var result='2';
        getExamineTable("/goodsTable/getExamineTable?result="+result+"&type="+3);
        changeColor3();
    });
    $("#b4").click(function () {
        var result='3';
        getExamineTable("/goodsTable/getExamineTable?result="+result+"&type="+3);
        changeColor4();
    });
    /*审核*/
    shenhe = function shenhe(productId,productName) {
        var obj;
        layer.open({
            type:2,
            title:'商品审核',
            content:"/goods/goods-commodity-audit.html",
            maxmin:true,
            area:["450px","340px"],
            btn:["确定","取消"],
            yes:function(index){
                var implementId = localStorage.getItem("createId");
                var implementDate = new Date();
                implementDate = getTime(implementDate);
                var result = localStorage.getItem("result");
                var jsCourseRecord =
                    {
                        result:result,
                        remarks:obj.find("#remarks").val(),
                        implementId:implementId,
                        courseName:implementDate,
                        id:obj.find("#id").val(),
                        type:"3"
                    };
                $.ajax({
                    type:"post",
                    url:"/goodsTable/insertRecord",
                    dataType:"json",
                    data:jsCourseRecord,
                    success:function (data) {
                        if(data){
                            alert("审核成功");
                            location.reload();
                        }else{
                            alert("审核失败");
                        }
                    }
                });
            },btn2:function(index){
                layer.close(index);
            },
            success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find("#shopName").text(productName);
                obj.find("#id").val(productId);
            }
        });
    }

    /*获取时间*/
    function getTime(date){
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        var today = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        var nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    }

    /*检索*/
    $("#jiansuo").click(function () {
        var obj;
        layer.open({
            type: 2,
            shade: 0,
            title:"高级检索",
            content:"/goods/goods-search.html",
            area: ['610px', '380px'],
            yes:function(index){
                var productCode = obj.find("#productCode").val();
                var title = obj.find("#title").val();
                var productName = obj.find("#productName").val();
                var currentpriceProduct = obj.find("#currentpriceProduct").val();
                var trueName = obj.find("#trueName").val();
                var result = obj.find("#result").val();
                select(
                    "/Record/selectGaoJi?productCode="+ productCode
                    +"&title="+title
                    +"&productName="+productName
                    +"&currentpriceProduct="+currentpriceProduct
                    +"&trueName="+trueName
                    +"&result="+result
                );
                layer.close(index);
            },btn2:function(index){
                layer.close(index);
            },
            success:function (layero,index) {
                obj = layero.find('iframe').contents();
            }
        })
    });

    /*查看商品详情*/
    chakan = function chakan(productId){
        $.ajax({
            url:"/goodsTable/getGoodsMsg",
            type:"post",
            dataType:"json",
            data:{"productId":productId},
            success:function(data){
                devicesMsg(data);
            },
        })
    }
    function devicesMsg(data){
        layer.open({
            type: 2,
            title:"商品审核详情",
            shade: 0,
            maxmin:true,
            content: "/goods/goods-see.html",
            area:["1020px","520px"],
            btn:["关闭"],
            yes:function(index){
                layer.close(index);
            },
            success:function(layero,index){
                var obj = layero.find('iframe').contents();
                obj.find("#productCode").val(data.data.productCode);
                obj.find("#productName").val(data.data.productName);
                obj.find("#currentpriceProduct").val(data.data.currentpriceProduct+"元");
                obj.find("#title").val(data.data.title);
                if (data.data.productSate == 0) {
                    obj.find("#productSate").val("已删除");
                } else if (data.data.productSate == 1) {
                    obj.find("#productSate").val("上架");
                } else if (data.data.productSate == 2) {
                    obj.find("#productSate").val("下架");
                } else {
                    return "";
                }
                obj.find('#saleNum').val(data.data.saleNum);
                if(data.data.saleNum==null){
                    obj.find("#saleNum").val("0");
                }
                obj.find("#hits").val(data.data.hits);
                obj.find("#collectionNum").val(data.data.collectionNum);
                if(data.data.collectionNum==null){
                    obj.find("#collectionNum").val("0");
                }
                obj.find("#productDescription").val(data.data.productDescription);
                obj.find("#productLabel").val(data.data.productLabel);
                obj.find("#mainImage").attr('src', "http://"+data.data.mainImage);
            }
        });
    };
    /*搜索*/
    $("#sousuo").click(function () {
        var productName = $("#productName").val();
        var title = $("#title").val();
        var trueName = $("#trueName").val();
        getExamineTable("/goodsTable/getExamineTable?productName="+productName+"&title="+title+"&trueName="+trueName);
    });
    /*重置*/
    $("#chongzhi").click(function () {
        $("#productName").val("");
        $("#title").val("");
        $("#trueName").val("");
        getExamineTable("/goodsTable/getExamineTable?type="+3);
    });

    /*刷新*/
    $("#shuaxin").click(function () {
        location.reload();
    });
});
/*显示数字*/
var saveNum=self.setInterval("giveNum()",1000);
/*显示数字*/
giveNum = function giveNum(){
    $("#b1").html("全部商品("+all+")");
    $("#b2").html("审核通过("+exmamineTrue+")");
    $("#b3").html("待审核("+waitExamine+")");
    $("#b4").html("审核未通过("+exmamineFalse+")");
    clearInterval(saveNum);
}


/*跳转*/
function goBack() {
    parent.$('#recordLog').val($('#examine').text());
    parent.before();
    return false;
}
